package blockQueue;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;



/*
    //元素结构，单向链表，
    static class Node<E> {
        E item;
        Node<E> next;
        Node(E x) { item = x; }
    }

    //元素数量
    private final AtomicInteger count = new AtomicInteger();

    //头结点
    //Invariant: head.item == null   ***链表头没有元素，head.next为真正的头元素
    transient Node<E> head;
    //尾结点
    private transient Node<E> last;

    //取元素的锁
    private final ReentrantLock takeLock = new ReentrantLock();
    private final Condition notEmpty = takeLock.newCondition();
    //存元素的锁
    private final ReentrantLock putLock = new ReentrantLock();
    private final Condition notFull = putLock.newCondition();

    //队列大小
    public int size() {
        return count.get();
    }
    //剩余大小
    public int remainingCapacity() {
        return capacity - count.get();
    }

    //阻塞存，如果队列满了，阻塞等等
    public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        putLock.lockInterruptibly();
        try {
            //等待容量
            while (count.get() == capacity) {
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();//通知其他等待容量线程
        } finally {
            putLock.unlock();
        }
        //如果容量从0到1，通知一个等待取数据的线程（被notEmpty阻塞了）
        //PS：容量为0的队列，取数据线程处于阻塞状态
        if (c == 0)
            signalNotEmpty();
    }

    //非阻塞存，如果容量满了，快速失败，
    //Inserts the specified element at the tail of this queue
    public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        final AtomicInteger count = this.count;
        if (count.get() == capacity)
            return false;
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        putLock.lock();
        try {
            if (count.get() < capacity) {
                enqueue(node);
                c = count.getAndIncrement();
                if (c + 1 < capacity)
                    notFull.signal();
            }
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
        return c >= 0;
    }

    //阻塞取
    public E take() throws InterruptedException {
        E x;
        int c = -1;
        final AtomicInteger count = this.count;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lockInterruptibly();
        try {
            while (count.get() == 0) {
                notEmpty.await();
            }
            x = dequeue();
            c = count.getAndDecrement();
            if (c > 1)
                notEmpty.signal();
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

    //非阻塞取，没有返回空
    public E poll() {
        final AtomicInteger count = this.count;
        if (count.get() == 0)
            return null;
        E x = null;
        int c = -1;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            if (count.get() > 0) {
                x = dequeue();
                c = count.getAndDecrement();
                if (c > 1)
                    notEmpty.signal();
            }
        } finally {
            takeLock.unlock();
        }
        if (c == capacity)
            signalNotFull();
        return x;
    }

    //获取头元素  但不删除
    public E peek() {
        if (count.get() == 0)
            return null;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();
        try {
            Node<E> first = head.next;
            if (first == null)
                return null;
            else
                return first.item;
        } finally {
            takeLock.unlock();
        }
    }

    //删除trail后的p
    void unlink(Node<E> p, Node<E> trail) {
        p.item = null;
        trail.next = p.next;
        if (last == p)
            last = trail;
        if (count.getAndDecrement() == capacity)
            notFull.signal();
    }
 */

public class LinkedBlockingQueueTest {

    public static void main(String[] args) {
        LinkedBlockingQueue<String> linkedBlockingQueue
                = new LinkedBlockingQueue<>();

        LinkedBlockingQueue<String> linkedBlockingQueue1
                = new LinkedBlockingQueue<>(10);

        List<String> objects = Collections.emptyList();
        LinkedBlockingQueue<String> linkedBlockingQueue2
                = new LinkedBlockingQueue<String>(objects);

    }
}
